package vincent.com.layouttest.surface;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import vincent.com.layouttest.R;
import vincent.com.layouttest.util.ImageUtil;
import vincent.com.layouttest.util.LogUtil;
import vincent.com.layouttest.view.RotateImageView;

/**
 * Created by zhengwenchen on 2016/4/22.
 */
public class RotateImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mSurfaceHolder;
    private DrawingThread mDrawingThread;

    private Drawable mDrawable;

    private float mAngle = 0f;

    public RotateImageSurfaceView(Context context) {
        this(context, null);
    }

    public RotateImageSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private int mPx;
    private int mPy;

    private int mWidth;
    private int mHeight;

    private boolean isPause = false;

    public RotateImageSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mDrawingThread = new DrawingThread();

        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.my_myalbum_img);

        Bitmap circle = ImageUtil.circleHead(bitmap, bitmap.getWidth(), bitmap.getHeight(), 0, Color.TRANSPARENT);

        mWidth = circle.getWidth();
        mHeight = circle.getHeight();

        mDrawable = new BitmapDrawable(getContext().getResources(), circle);

        mDrawable.setBounds(0, 0, mWidth, mHeight);

        try {
            bitmap.recycle();
            bitmap = null;
        }catch (Exception e){
            Log.e("czw_exception", e.getMessage());
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mDrawingThread.keepRunning = true;
        mDrawingThread.setPriority(Thread.MAX_PRIORITY);
        mDrawingThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        LogUtil.i("czw_rotate_surface", "surfaceChanged===>width: " + width + " | height: " + height);
        mPx = width/2;
        mPy = height/2;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mDrawingThread.keepRunning = false;
    }

    private class DrawingThread extends Thread {
        boolean keepRunning = true;

        @Override
        public void run() {
            Canvas c;
            while (keepRunning) {
                c = null;

                try {
                    c = mSurfaceHolder.lockCanvas();
                    synchronized (mSurfaceHolder) {
                        mAngle += 0.306f;
                        draw(c);
                    }
                } finally {
                    if (c != null)
                        mSurfaceHolder.unlockCanvasAndPost(c);
                }

                // Run the draw loop at 50FPS
                try {
                    Thread.sleep(17);
                } catch (InterruptedException e) {}
            }
        }
    }

    @Override
    public void draw(Canvas canvas) {
        if(canvas == null) {
            return;
        }
        super.draw(canvas);
        mAngle = mAngle % 360;
        canvas.rotate(mAngle, mPx, mPy);
        mDrawable.draw(canvas);
    }

    public void setPause(boolean isPause){
        this.isPause = isPause;
    }
}
